package org.begincode.task.queue;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.begincode.blog.service.BlogService;
import org.begincode.code.service.CodeService;
import org.begincode.core.model.*;
import org.begincode.course.service.CourseService;
import org.begincode.resource.service.ResourceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
@Async
public class CountOperator
{
	Logger logger = LoggerFactory.getLogger(CountOperator.class);
	public static  int MAX_SIZE = 4;
	@Autowired
	CodeService codeService;

	@Autowired
	BlogService blogService;

	@Autowired
	ResourceService resourceService;

	@Autowired
	CourseService courseService;



	public void changeBatViewCount()
	{
		updateBatchChangeCodeViewCount();
		updateBatchChangeBlogViewCount();
		updateBatchChangeResourceViewCount();
		updateBatchChangeCourseViewCount();
	}

	public void addCourseViewCount(int courseId) {
		int queueCount = CountQueue.addCourseViewQueue(Integer.valueOf(courseId));
		if (queueCount >= MAX_SIZE)
			changeBatViewCount();
	}

	public void addResourceViewCount(int resourceId) {
		int queueCount = CountQueue.addResourceViewQueue(Integer.valueOf(resourceId));
		if (queueCount >= MAX_SIZE)
			changeBatViewCount();
	}

	public void addCodeViewCount(int codeId)
	{
		int queueCount = CountQueue.addCodeViewQueue(Integer.valueOf(codeId));
		if (queueCount >= MAX_SIZE)
		{
			changeBatViewCount();
		}
	}



	public void addBlogViewCount(int blogId) {
		int queueCount = CountQueue.addBlogViewQueue(Integer.valueOf(blogId));
		if (queueCount >= MAX_SIZE)
		{
			changeBatViewCount();
		}
	}



	private void updateBatchChangeCodeViewCount()
	{
		List list = new ArrayList();
		Map<Integer,Integer> codeMap = CountQueue.getCodeViewQueue();
		for (Integer key : codeMap.keySet()) {
			BegincodeCode code = new BegincodeCode();
			code.setViewCount((Integer)codeMap.get(key));
			code.setbegincodeCodeId(key);
			list.add(code);
		}
		if ((list != null) && (list.size() > 0))
			this.codeService.batchUpdateAddCountById(list);
	}

	private void updateBatchChangeBlogViewCount() {
		List list = new ArrayList();
		Map<Integer,Integer> codeMap = CountQueue.getBlogViewQueue();
		for (Integer key : codeMap.keySet()) {
			Blog blog = new Blog();
			blog.setViewCount((Integer) codeMap.get(key));
			blog.setBlogId(key);
			list.add(blog);
		}
		if ((list != null) && (list.size() > 0))
			this.blogService.batchUpdateAddCountById(list);
	}

	private void updateBatchChangeResourceViewCount()
	{
		List list = new ArrayList();
		Map<Integer,Integer> resourceMap = CountQueue.getResourceViewQueue();
		for (Integer key : resourceMap.keySet()) {
			Resource resource = new Resource();
			resource.setDownCount((Integer) resourceMap.get(key));
			resource.setResourceId(key);
			list.add(resource);
		}
		if ((list != null) && (list.size() > 0))
			this.resourceService.batchUpdateAddCountById(list);
	}
	private void updateBatchChangeCourseViewCount() {
		List list = new ArrayList();
		Map<Integer,Integer> courseMap = CountQueue.getCourseViewQueue();
		for (Integer key : courseMap.keySet()) {
			Course course = new Course();
			course.setAttentionCount((Integer) courseMap.get(key));
			course.setCourseId(key);
			list.add(course);
		}
		if ((list != null) && (list.size() > 0))
			this.courseService.batchUpdateAddCountById(list);
	}
}